package com.thrsdk.common.network.result;

import android.text.TextUtils;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 返回数据基类
 * 服务器端返回的统一数据格式
 * {
 * "id":"1543202645289876",
 * "state":{"code":0,"msg":"操作成功"},
 * "data":{} // 数据
 * "etag":"f9e0c242d912297c8189b365748aaa43"
 * }
 */

public class BaseBean<T> implements Serializable {
    private String status;

    private String msg;
    private String code;
    private T data;
    public Class<T> tClass;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public boolean isResult() {
        return TextUtils.equals(getStatus(), "success");
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public BaseBean() {
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericSuperclass;
            tClass = (Class<T>) pt.getActualTypeArguments()[0];
        }
    }

    public Class<T> getClassOfT() {
        Class<T> tClass = null;
        try {
            Type superClass = getClass().getGenericSuperclass();
            if (superClass instanceof ParameterizedType)
                tClass = (Class<T>) ((ParameterizedType) superClass).getActualTypeArguments()[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tClass;
    }
}
